package top.itaso.app.ui.tts;

import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Locale;

import top.itaso.app.R;
import top.itaso.lib.alog.ALog;

/**
 * @Description: 与TTS有关功能的使用，如自带的TTS：TextToSpeak、其他第三方TTS功能等
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2022/04/13
 */
public class TTSFragment extends Fragment {
    private static final String TAG = "TTSFragment";
    private TTSViewModel mViewModel;

    private TextToSpeech mTts;
    private TextView mTvTtsStatus;
    private TextView mBtnTtsSpeak;
    private EditText mEdtSpeakContent;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(TTSViewModel.class);
        View rootView = inflater.inflate(R.layout.fragment_tts, container, false);
        mTvTtsStatus = rootView.findViewById(R.id.tv_tts_status);
        mBtnTtsSpeak = rootView.findViewById(R.id.tv_tts_speak);
        mEdtSpeakContent = rootView.findViewById(R.id.et_speak_content);

        mTts = new TextToSpeech(getContext(), mTtsListener);

        mBtnTtsSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String speakContent = mEdtSpeakContent.getText().toString();
                if (TextUtils.isEmpty(speakContent)) {
                    return;
                }
                mTts.speak(speakContent, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
        return rootView;
    }

    @Override
    public void onDestroyView() {
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroyView();
    }

    private TextToSpeech.OnInitListener mTtsListener = new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            String tips = getResources().getString(R.string.tip_format);
            if (status == TextToSpeech.SUCCESS) {
                //设置首选语言为中文,注意，语言可能是不可用的，结果将指示此
                int result = mTts.setLanguage(Locale.CHINA);
                mTts.setPitch(1.0f);// 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
                mTts.setSpeechRate(1.0f);//设置语速，值越大，语速越快
                if (result == TextToSpeech.LANG_MISSING_DATA ||
                        result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    //语言数据丢失或不支持该语言。
                    mTvTtsStatus.setText(String.format(tips,"当前中文语言数据丢失或不支持该语言"));
                } else {
                    //检查文档中其他可能的结果代码。
                    // 例如，语言可能对区域设置可用，但对指定的国家和变体不可用
                    // TTS引擎已成功初始化。
                    // 允许用户按下按钮让应用程序再次发言。
                    mBtnTtsSpeak.setEnabled(true);
                    mTvTtsStatus.setText(String.format(tips,"TTS已初始化完成"));
                }
            } else {
                mBtnTtsSpeak.setEnabled(false);
                mTvTtsStatus.setText(String.format(tips, "TTS初始化失败"));
            }
        }
    };
}